#ifndef _SYS_H
#define _SYS_H

#define CCLK ((unsigned long)60000000)		// processor clock (60Mhz)
#define PCLK ((unsigned long)15000000)		// peripheral clock (15Mhz)

#define CS0 7
#define CS1 16
// Changed PCLK to 15MHz for SPI purposes - william

// Initialize the system including PLL, high speed GPIO, interrupts,
// and the memory acceleration module. This should be the very first
// function called in main() of every LPC2103 program
void sys_init(void);

// delay for the specified number of milliseconds using timer0
void delay_ms_t0(int ms);

// delay for the specified number of microseconds using timer0
void delay_us_t0(int us);

// delay for the specified number of milliseconds using timer1
void delay_ms_t1(int ms);

// delay for the specified number of microseconds using timer1
void delay_us_t1(int us);

// Initialize uart0
void uart0_init(void);

// write a null terminated string out to uart0
void uart0_puts(const char *s);

// write out len bytes of raw data pointed to by d
void uart0_write(const char *d, int len);

// Initialize SPI0 interface
void spi_init(void);

// Used for CS on longer transfers
void spi_on(int cs);
void spi_off(int cs);

// Shifts one character out, and 1 byte of data in.
char spi_send(unsigned char spi_Data);

// SPI write command
// CS is cable select. Should be the port number of the pin used 
void spi_write(unsigned char spi_Data, int cs);

// Pulse the LED for the specified number of milliseconds
// SIDE EFFECTS: This function uses timer0 for the delay
void led_pulse(int ms);

// blink the LED at the specified rate. ms is the period of blinking in milliseconds
// This function returns immediately and uses timer1 to generate an interrupt at
// the blink rate. Call led_blink(0) to cancel the blinking
void led_blink(int ms);

#define ISR_ENTRY() asm volatile(" sub   lr, lr,#4\n" \
                                 " stmfd sp!,{r0-r12,lr}\n" \
                                 " mrs   r1, spsr\n" \
                                 " stmfd sp!,{r1}")
								 
#define ISR_EXIT()  asm volatile(" ldmfd sp!,{r1}\n" \
                                 " msr   spsr_c,r1\n" \
                                 " ldmfd sp!,{r0-r12,pc}^")

unsigned enableIRQ(void);
unsigned disableIRQ(void);
unsigned restoreIRQ(unsigned oldCPSR);
unsigned enableFIQ(void);
unsigned disableFIQ(void);

void UNDEF_Routine(void) __attribute__ ((interrupt ("UNDEF")));
void SWI_Routine(void)   __attribute__ ((interrupt ("SWI")));
void PAbt_Routine(void)  __attribute__ ((interrupt ("PAbt")));
void DAbt_Routine(void)  __attribute__ ((interrupt ("DAbt")));


// the user uncomment the following #define if you wish to 
// supply your own FIQ function
//#define USER_FIQ
void FIQ_Routine(void)   __attribute__ ((interrupt ("FIQ")));



#endif // _SYS_H
